﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ScreenMonitor.Common;
using ScreenMonitor.Common.Objects;
using ScreenMonitor.ClientLib;

namespace ClientUI
{
    public partial class SignWnd : Form
    {
        public static bool IsShowed = false;
        int SH, SW;
        int SignLimitTime;
        int SignLeftTime;
        delegate void HideDel();
        delegate void ShowDel(SignRequestObject obj);

        public SignWnd()
        {
            InitializeComponent();
        }

        public void ShowMe(object obj)
        {
            if (this.InvokeRequired)
            {
                ShowDel del = new ShowDel(ShowMe);
                this.BeginInvoke(del);
            }
            else
            {
                IsShowed = false;
                SignRequestObject obj1 = (SignRequestObject)obj;
                this.SignLimitTime = obj1.SignLimitTime;
                if (obj1.IsLockScreen)
                {
                    this.Top = 0;
                    this.Left = 0;
                    SH = (int)System.Windows.SystemParameters.FullPrimaryScreenHeight;
                    SW = (int)System.Windows.SystemParameters.FullPrimaryScreenWidth;
                    this.WindowState = FormWindowState.Maximized;
                    this.FormBorderStyle = FormBorderStyle.None;
                    this.Height = SH;
                    this.Width = SW;
                }
                else
                {
                    this.Top = (SH - this.Height) / 2;
                    this.Left = (SW - this.Width) / 2;
                    this.Size = panel1.Size;
                    this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
                    this.WindowState = FormWindowState.Normal;
                }
                this.BringToFront();
                this.TopMost = true;
                SignLeftTime = SignLimitTime;
                timer1.Start();
                this.ShowDialog();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.btngo.InvokeRequired)
            {
                EventHandler del = new EventHandler(timer1_Tick);
                this.btngo.BeginInvoke(del, sender, e);
            }
            else
            {
                if (IsShowed)
                    this.Close();

                SignLeftTime--;
                if (SignLeftTime > 0)
                {
                    this.lTimeLeft.Text = "剩余时间" + SignLeftTime + "s";
                    btngo.Text = "签到";

                }
                else
                {
                    this.lTimeLeft.Text = "签到超时！";
                    btngo.Text = "关闭";
                }
                this.TopMost = true;
                this.BringToFront();
            }
        }

        private void btngo_Click(object sender, EventArgs e)
        {
            if (btngo.Text == "签到")
            {
                SignResponceObject obj = new SignResponceObject();
                obj.Name = txtStuName.Text;
                obj.StuID = txtStuID.Text;
                ServerObject.SignCompelete(obj);
            }
            this.timer1.Stop();
            Hide();
        }

    }
}
